<?php

namespace App\Model\Dao;

use App\Utils\CustomUtils;
use App\Concern\BaseDao;
use Hyperf\Database\Query\Builder;
use Hyperf\DbConnection\Db;
use Hyperf\Utils\Arr;
use Redis;
use App\Utils\RedisUtils;
use Hyperf\Utils\Codec\Json;

/**
 * ChannelInfoLogDao
 */
class ChannelInfoLogDao extends BaseDao {

    /**
     * 表名常量
     * 联表的时候通过XxxDao::TABLE获取
     */
    const TABLE = "channel_info_log";

    // 不支持软删除
    protected $softDelete = null;

    /**
     * 父类中通过静态函数获取表名, 因为不能通过静态常量获取, 这个函数是固定写在子类中的
     */
    public static function table (): string {
        return self::TABLE;
    }


    /**
     * 排序
     *
     * @param Builder $builder
     * @param array   $params
     *
     * @return Builder|mixed
     */
    protected function _buildOrder (Builder $builder, $params = []) {

        $builder->orderBy("main.createTime", "desc");
        $builder->orderBy("main.id", "asc");

        return $builder;
    }

    /**
     * 查询条件
     *
     * @param $params
     *
     * @return Builder
     */
    protected function _buildQueryString ($params) {

        // 本表一律命名为main
        $main = sprintf("%s AS main", self::TABLE);

        $bank = sprintf('%s AS bank', BankInfoDao::TABLE);
        $riskCollection = sprintf('%s AS riskCollection', 'risk_rule_collection');

        $builder = DB::table($main);
        $builder->leftJoin($bank, 'bank.id', 'main.bankId');
        $builder->leftJoin($riskCollection, 'riskCollection.id', 'main.riskCollectionId');

        $builder->selectRaw(join(',', [
            'main.*',
            'bank.bankName',
            'riskCollection.collectionName AS riskCollectionName',
        ]));

        Arr::exists($params, 'channelId') && $builder->where('main.channelId', $params['channelId']);

        return $builder;
    }


}
